package com.konsung.ksdc.manager.user;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.konsung.ksdc.R;
import com.konsung.ksdc.common.utils.DensityUtils;
import com.konsung.ksdc.common.utils.QRCodeUtils;
import com.konsung.ksdc.common.utils.StringUtils;
import com.konsung.ksdc.manager.appmain.BaseActivity;
import com.squareup.picasso.Picasso;

import java.io.File;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 二维码名片页面
 * Created by DJH on 2017/5/2 0002.
 */

public class QRCodeActivity extends BaseActivity {
    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.iv_qr_code)
    ImageView ivQrCode;
    @Bind(R.id.iv_user_head)
    ImageView ivUserHead;
    @Bind(R.id.tv_user_name)
    TextView tvUserName;
    @Bind(R.id.tv_nick_name)
    TextView tvNickName;
    @Bind(R.id.tv_hint)
    TextView tvHint;

    private String userName; //唯一的账号或id
    private String pic; //头像
    private String nickName; //昵称
    private boolean isfromGroup; //二维码来自个人或群
    private Bitmap headPhoto;

    @Override
    protected String activityDesc() {
        return getString(R.string.qr_code);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qr_code);
        ButterKnife.bind(this);
        isfromGroup = getIntent().getBooleanExtra("from", false);
        userName = getIntent().getStringExtra("name");
        userName = getIntent().getStringExtra("name");
        pic = getIntent().getStringExtra("pic");
        nickName = getIntent().getStringExtra("nick");
        initEvent();
        initView();
        createQRCode(userName, null);
    }

    /**
     * 初始化二维码页面
     */
    private void initView() {

        // 头像
        String picUrl = StringUtils.appendPicPath(pic);
//        userName = user.getUserName();
        //暂时屏蔽二维码中间添加图片
//        if (null != user.getUserPic() && !user.getUserPic().equals("") && !ivUserHead
//                .getDrawable().getClass().equals(NinePatchDrawable.class)) {
//            headPhoto = ((BitmapDrawable) ivUserHead.getDrawable()).getBitmap();
//        } else {
//            headPhoto = null;
//        }
        if (!isfromGroup) {
            //只显示个人了
            tvUserName.setText(userName);
            tvHint.setText(R.string.scan_qr_code);
            Picasso.with(this)
                    .load(picUrl)
                    .placeholder(R.drawable.pic_talkbox_sel)
                    .error(R.drawable.pic_talkbox_sel)
                    .into(ivUserHead);
        } else {
            //群的
            tvHint.setText(R.string.scan_qr_group);
            Picasso.with(this)
                    .load(picUrl)
                    .placeholder(R.drawable.pic_head_defult)
                    .error(R.drawable.pic_head_defult)
                    .into(ivUserHead);
        }
        tvNickName.setText(nickName);
    }

    /**
     * 初始化二维码页面事件
     */
    private void initEvent() {
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    /**
     * 根据数据生成二维码图片
     * @param userName 用户名
     * @param photo 头像
     */
    private void createQRCode(final String userName, final Bitmap photo) {
        final String filePath = QRCodeUtils.getFileRoot(QRCodeActivity.this) + File.separator
                + "qr_" + System.currentTimeMillis() + ".jpg";
        new Thread(new Runnable() {
            @Override
            public void run() {
                boolean success = QRCodeUtils.createQRImage(userName, DensityUtils
                        .dp2px(QRCodeActivity.this, 178), DensityUtils.dp2px(QRCodeActivity
                        .this, 178), photo, filePath);
                if (success) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ivQrCode.setImageBitmap(BitmapFactory.decodeFile(filePath));
                        }
                    });
                }
            }
        }).start();
    }
}
